 package com.bjsxt.shop.goods.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.common.util.RedisUtil;
import com.bjsxt.shop.goods.dao.IAGoodsBrandDao;
import com.bjsxt.shop.goods.dao.IAGoodsCateDao;
import com.bjsxt.shop.goods.dao.IAGoodsDao;
import com.bjsxt.shop.goods.dao.IAUsersCarDao;
import com.bjsxt.shop.goods.pojo.AGoods;
import com.bjsxt.shop.goods.pojo.AGoodsBrand;
import com.bjsxt.shop.goods.pojo.AGoodsCate;
import com.bjsxt.shop.goods.pojo.AUsersCar;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements IGoodsService
{
	@Resource
	private IAGoodsBrandDao goodsBrandDao;
	@Resource
	private IAGoodsCateDao goodsCateDao;
	@Resource
	private IAGoodsDao goodsDao;
	@Resource
	private IAUsersCarDao usersCarDao;
	@Resource
	private RedisUtil redisUtil;
	
	@Override
	public AGoodsBrand findOneGoodsBrandService(Map<String, Object> condMap)
	{
		return this.goodsBrandDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.save(goodsBrand);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject saveOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		/* 保存品牌 */
		JSONObject resultJSON = this.saveOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsBrandDao.update(goodsBrand);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsBrand.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsBrandService(AGoodsBrand goodsBrand,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.updateOneGoodsBrandService(goodsBrand);
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsBrandService(AGoodsBrand goodsBrand)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String,Object> findCondListGoodsBrandService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* 分页 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AGoodsBrand> roleList = this.goodsBrandDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AGoodsBrand> roleList= this.goodsBrandDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}
	
	@Override
	public AGoodsCate findOneGoodsCateService(Map<String, Object> condMap)
	{
		AGoodsCate goodsCate=this.goodsCateDao.findOne(condMap);
		return goodsCate;
	}
	
	@Override
	public JSONObject saveOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.save(goodsCate);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsCateDao.update(goodsCate);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goodsCate.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsCateService(AGoodsCate goodsCate)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String,Object> findCondListGoodsCateService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap=new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoodsCate> roleList = this.goodsCateDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List roleList = this.goodsCateDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap;
	}
	/*查询树形结构平板样式*/
	@Override
	public List<AGoodsCate> findTreeListCateService()
	{
		List<AGoodsCate> resultList=new ArrayList<AGoodsCate>();
		/*先查询出所有的一级节点*/
		Map<String, Object> condMap=new HashMap<>();
		condMap.put("parentId", "0");
		Map<String, Object> resultMap=this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> cateList=(List<AGoodsCate>) resultMap.get("list");
		for (Iterator iterator = cateList.iterator(); iterator.hasNext();)
		{
			AGoodsCate cateTemp = (AGoodsCate) iterator.next();
			this.findTreeListGateServiceUtil(cateTemp, 0, resultList);
		}
		return resultList;
	}
	
	/*列出结构的递归方法*/
	private void findTreeListGateServiceUtil(AGoodsCate cate,int level,List<AGoodsCate> goodsGateList)
	{
		StringBuffer preStrSb=new StringBuffer();
		for (int i = 0; i < level; i++)
		{
			preStrSb.append("|-->");
		}
		/**
		 * 列出树形结构
		 */
		cate.setTreeName(preStrSb.toString()+cate.getName());
		/*添加到容器中*/
		goodsGateList.add(cate);
		/* 查询子节点 */
		Map<String, Object> condMap=new HashMap<>();
		condMap.put("parentId", cate.getId());
		Map<String, Object> resultMap=this.findCondListGoodsCateService(null, condMap);
		List<AGoodsCate> childList=(List<AGoodsCate>) resultMap.get("list");
		if(childList.size()>0)
		{
			cate.setLeafStatus("0");
			for (Iterator iterator = childList.iterator(); iterator.hasNext();)
			{
				AGoodsCate cateTmpe = (AGoodsCate) iterator.next();
				this.findTreeListGateServiceUtil(cateTmpe, level+1, goodsGateList);
				
			}
		}else
		{
			/* 叶子节点 */
			cate.setLeafStatus("1");
		}
	}
	
	public AGoods findOneGoodsService(Map<String, Object> condMap)
	{
		return this.goodsDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.save(goods);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	/**
	 * 保存一条商品
	 * @param goods	保存的对象
	 * @return	json:{"code":"0","info":"成功","data":{"effect":"","id":""}}
	 */
	public JSONObject saveOneGoodsService(AGoods goods,Map<String, Object> paramsMap)
	{
		JSONObject resultJSON = this.saveOneGoodsService(goods);
		return resultJSON ; 
	}
	
	@Override
	public JSONObject updateOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.goodsDao.update(goods);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", goods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneGoodsService(AGoods goods)
	{
		JSONObject resultJSON = new JSONObject();
		return resultJSON;
	}
	
	@Override
	public Map<String,Object> findCondListGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 和搜索条件相关*/
		String redisKey = "goodsList_" + condMap.get("cateId") + "_" + pageInfoUtil.getCurrentPage() ; 
		
		Map<String, Object> resultMap=new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			/* --------------从Redis中取数据-------------- */
			String resultJSONstr=this.redisUtil.get(redisKey);
			if(resultJSONstr!=null&&!"".equalsIgnoreCase(resultJSONstr)){
				JSONObject resultJSON=(JSONObject) JSON.parse(resultJSONstr);
				if(resultJSON!=null&&"0".equalsIgnoreCase(resultJSON.getString("code")+""))
				{
					/* 数据成功 */
					JSONObject dataJSON = (JSONObject) resultJSON.get("data") ;
					/* 分页信息 */
					JSONObject pageInfoJSON=(JSONObject) dataJSON.get("pageInfo");
					/* pageInfoUtil应该提供一个将json转换成对象的方法 */
					pageInfoUtil = new PageInfoUtil() ; 
					pageInfoUtil = pageInfoUtil.parseJSON(pageInfoJSON);
					resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
					
					List<AGoods> goodsList = new ArrayList();
					/* 数据 */
					JSONArray goodsArr = (JSONArray) dataJSON.get("goodsList");
					for (Iterator iterator = goodsArr.iterator(); iterator.hasNext();)
					{
						JSONObject goodsJSON = (JSONObject) iterator.next();
						AGoods goods = new AGoods() ; 
						goods = goods.parseJSON(goodsJSON);
						goodsList.add(goods);
					}
					resultMap.put("list", goodsList);
					return resultMap ; 
				}
			}
			/* 分页 */
			/* 查询总记录数 */
			Page page=PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AGoods> goodsList = this.goodsDao.findList(condMap);
			resultMap.put("list", goodsList);
			/*设置总记录数*/
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			/* ---------往Redis中放一次 --------- */
			/*
			 * {
				“code”:”0”,
				“info:””,
				“data”:
				{
				   “goodsList”:
					[
					{}
					]
					},
					“pageInfo”:{
					}
				}
			 */
			JSONObject resultJSON=new JSONObject();
			resultJSON.put("code", 0);
			resultJSON.put("info", ConstatFinalUtil.CONFIG_JSON.getString("0"));
			JSONObject dataJSON=new JSONObject();
			JSONArray goodsArr=new JSONArray();
			for (Iterator iterator = goodsList.iterator(); iterator.hasNext();)
			{
				AGoods goods = (AGoods) iterator.next();
                goodsArr.add(goods.toJSON());
			}
			dataJSON.put("goodsList", goodsArr);
			dataJSON.put("pageInfo", pageInfoUtil.toJSON());
			resultJSON.put("data", dataJSON);
			this.redisUtil.set("redisKey", resultJSON.toJSONString(),
					Integer.valueOf(ConstatFinalUtil.CONFIG_JSON.get("password.error.lockTime") + ""));
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AGoods> goodsList = this.goodsDao.findList(condMap);
		resultMap.put("list", goodsList);
		return resultMap;
	}

	@Override
	public AUsersCar findOneUsersCarService(Map<String, Object> condMap)
	{
		return this.usersCarDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneUsersCarService(AUsersCar usersCar)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersCarDao.save(usersCar);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersCar.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneUsersCarService(AUsersCar usersCar)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.usersCarDao.update(usersCar);
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", usersCar.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneUsersCarService(AUsersCar usersCar)
	{
		return null;
	}
	
	@Override
	public Map<String, Object> findCondListUsersCarService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String,Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage() ,pageInfoUtil.getPageSize());
			List<AUsersCar> roleList = this.usersCarDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap ; 
		}
		/* 不分页查询数据 */
		List<AUsersCar> carList = this.usersCarDao.findList(condMap);
		resultMap.put("list", carList);
		return resultMap ; 
	}
}
